package Chapter1.link;

public class OperationLinkNode {
    static class DoublyListNode {
        int val;
        DoublyListNode next, prev;

        DoublyListNode(int x) {
            val = x;
        }
    }

    static DoublyListNode createDoublyLinkedList(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        DoublyListNode head = new DoublyListNode(arr[0]);
        DoublyListNode cur = head;
        // for 循环迭代创建双链表
        for (int i = 1; i < arr.length; i++) {
            DoublyListNode newNode = new DoublyListNode(arr[i]);
            cur.next = newNode;
            newNode.prev = cur;
            cur = cur.next;
        }
        return head;
    }

    public static void main(String[] args) {
        deleteTail();
    }

    public static void addHead(int value) {
        DoublyListNode node = createDoublyLinkedList(new int[]{1, 2, 3, 4, 4, 5});
        DoublyListNode newNode = new DoublyListNode(value);
        newNode.next = node;
        node.prev = newNode;
        node = newNode;
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static void addTail(int value) {
        DoublyListNode node = createDoublyLinkedList(new int[]{1, 2, 3, 4, 4, 5});
        DoublyListNode tail = node;
        while (tail.next != null) {
            tail = tail.next;
        }
        DoublyListNode newNode = new DoublyListNode(value);
        newNode.prev = tail;
        tail.next = newNode;
        tail = newNode;
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static void addMid(int value, int x) {
        DoublyListNode node = createDoublyLinkedList(new int[]{1, 2, 3, 4, 4, 5});
        DoublyListNode xNode = node;
        for (int i = 0; i < x; i++) {
            xNode = xNode.next;
        }
        DoublyListNode newNode = new DoublyListNode(value);
        // 先组装新节点
        // 新节点的next指向x节点的next
        newNode.next = xNode.next;
        // 新节点的prev指向x节点
        newNode.prev = xNode;

        xNode.next.prev = newNode;
        xNode.next = newNode;
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static void deleteIndex(int index) {
        DoublyListNode node = createDoublyLinkedList(new int[]{1, 2, 3, 4, 4, 5});
        DoublyListNode deletePrvNode = node;
        for (int i = 0; i < index - 1; i++) {
            deletePrvNode = deletePrvNode.next;
        }
//        DoublyListNode toDeleteNode = deletePrvNode.next;
//        toDeleteNode.next.prev = deletePrvNode;
//        deletePrvNode.next = toDeleteNode.next;

        deletePrvNode.next = deletePrvNode.next.next;
        deletePrvNode.next.next.prev = deletePrvNode;

        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static void deleteTail() {
        DoublyListNode node = createDoublyLinkedList(new int[]{1, 2, 3, 4, 4, 5});
        DoublyListNode deletePrvNode = node;
        while (deletePrvNode.next != null) {
            deletePrvNode = deletePrvNode.next;
        }
        deletePrvNode.prev.next = null;

        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
}
